<?php

namespace app\controller;

use app\BaseController;

class Wangyiyun extends BaseController
{
    /**
     * 视图
     *
     * @return void
     */
    public function index()
    {
        $data = request()->param();
        $url = urldecode($data['url']);
        if (!@file_get_contents($url)) {
            error('音乐链接已失效，请重新解析');
        }
        return view('', [
            'data' => $data
        ]);
    }

    /**
     * 下载
     *
     * @return void
     */
    public function download()
    {
        $data = request()->param();
        //文件路径（路径+文件名）
        $file_name = $data['url'];
        $name = $data['name'] ? $data['name'] : basename($file_name);
        $name = '网易云音乐_' . $name;
        // required
        header('Pragma: public');
        //no cache
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private', false);
        //强制下载
        header('Content-Type:application/force-download');
        header('Content-Disposition: attachment; filename="' . $name . '"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: close');
        //输出到浏览器
        readfile($file_name);
        exit();
    }
}
